<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
<title>添加多边形区域覆盖物</title>
<style type="text/css">
html,body{
    width:100%;
    height:100%;
}
#container{
    width:100%;
    height:100%;
}
*{
    margin:0px;
    padding:0px;
}
body, button, input, select, textarea {
    font: 12px/16px Verdana, Helvetica, Arial, sans-serif;
}
#info{
    width:603px;
    padding-top:3px;
    overflow:hidden;
}
.btn{
    width:190px;
}
</style>
<script charset="utf-8" src="https://map.qq.com/api/js?v=2.exp&key=EJDBZ-GHO3D-MB24R-HJ3FZ-2B3Y6-52FBX"></script>
<script>
	
// https://lbs.qq.com/webApi/component/componentGuide/componentGeolocation	
	
var init = function() {
    var center = new qq.maps.LatLng(39.916527,116.397128);
    var map = new qq.maps.Map(document.getElementById('container'),{
        center: center,
        zoom: 13
    });
     var path1=[
      
        new qq.maps.LatLng(39.912106,116.389332),
        new qq.maps.LatLng(39.910658,116.390018),
        new qq.maps.LatLng(39.908354,116.389332),
        new qq.maps.LatLng(39.907234,116.387014),
        new qq.maps.LatLng(39.906839,116.384611),
        new qq.maps.LatLng(39.910329,116.382380),
        new qq.maps.LatLng(39.915661,116.384354),
        new qq.maps.LatLng(39.915859,116.385813)
    ];
	//添加覆盖物
    var polygon = new qq.maps.Polygon({
        path:path1,
        strokeColor: '#000000',
        strokeWeight: 5,
        fillColor: '#111111',
        map: map
    });
	//添加Marker
   var pointArr=[
		{latitude:39.912106,longitude:116.389332},
		{latitude:39.910658,longitude:116.390018},
		{latitude:39.908354,longitude:116.389332},
		{latitude:39.907234,longitude:116.387014},
		{latitude:39.906839,longitude:116.384611},
		{latitude:39.910329,longitude:116.382380},
		{latitude:39.915661,longitude:116.384354},
		{latitude:39.915859,longitude:116.385813}
	   ]
	qq.maps.event.addListener(map, 'click', function(evt) {
	        //console.log(evt);
			var lat = evt.latLng.getLat().toFixed(6);
			var lng = evt.latLng.getLng().toFixed(6);
			var str = '{latitude:'+lat+',longitude:'+lng +'},'
			//console.log(str);
			//var inarea=polygon.getBounds().contains(new qq.maps.LatLng(lat,lng));
			var inarea = IsPtInPoly(lat , lng , pointArr)
			console.log(inarea)
			
	    });
	
		
	
function IsPtInPoly(aLat, aLon, pointList) {
  /* 
  :param aLon: double 经度 
  :param aLat: double 纬度 
  :param pointList: list [{latitude: 22.22, longitude: 113.113}...] 多边形点的顺序需根据顺时针或逆时针，不能乱 
  */
  var iSum = 0  
  var iCount = pointList.length
    
  if(iCount < 3) {
      return false 
  }
  //  待判断的点(x, y) 为已知值
  var y = aLat
  var x = aLon
  for(var i = 0; i < iCount; i++) {
      var y1 = pointList[i].latitude  
      var x1 = pointList[i].longitude
      if(i == iCount - 1) {
          var y2 = pointList[0].latitude
          var x2 = pointList[0].longitude
      } else {
          var y2 = pointList[i + 1].latitude  
          var x2 = pointList[i + 1].longitude
      }
      // 当前边的 2 个端点分别为 已知值(x1, y1), (x2, y2)
      if (((y >= y1) && (y < y2)) || ((y >= y2) && (y < y1))) {
          //  y 界于 y1 和 y2 之间
          //  假设过待判断点(x, y)的水平直线和当前边的交点为(x_intersect, y_intersect)，有y_intersect = y
          // 则有（2个相似三角形，公用顶角，宽/宽 = 高/高）：|x1 - x2| / |x1 - x_intersect| = |y1 - y2| / |y1 - y|
          if (Math.abs(y1 - y2) > 0) {
              var x_intersect = x1 - ((x1 - x2) * (y1 - y)) / (y1 - y2);  
              if(x_intersect < x) {
                  iSum += 1 
              }
          }
      } 
  }
  if(iSum % 2 != 0) {
      return true  
  }else {
      return false 
  }  
}		
		
		
		
}
</script>
</head>
<body onload="init()">
<div id="container"></div>

</body>
</html>


